package fr.auzaiphe.joueur.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.SpringLayout;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;

import fr.auzaiphe.admin.model.ChronoGeneral;
import fr.auzaiphe.commun.NonEditableDefaultTableModel;
import fr.auzaiphe.commun.Outils;
import fr.auzaiphe.joueur.controller.JoueurController;
import fr.auzaiphe.joueur.model.JoueurModel;

public class JoueurPanelConnecte extends JPanel
{
	private JTextField textField;
	private JTable table;
	private JoueurController controller;

	private SpringLayout layout;
	private JPanel panelCrono;
	private SpringLayout layoutTextCrono;
	private JLabel textCronoGeneral;
	private JLabel textCronoJoueur;
	private JFormattedTextField fieldMinute;

	private JButton btnEnvoyer;
	private JScrollPane scrollPaneChat;
	private JTextArea zoneTexte;

	/*private JLabel labelCam;
	private JButton btnOnCam;*/

	private JTabbedPane onglets;
	private JPanel ongletIndice;
	private SpringLayout layoutOngletIndice;

	private JScrollPane paneTablIndice;
	private DefaultTableModel tableur;

	private JPanel panelAchatIndice;
	private SpringLayout layoutAchatIndice;
	private JTextPane txtpnAcheterUnIndice;
	private JButton btnValiderAchatIndice;

	/*private JPanel ongletTemps;
	private SpringLayout layoutOngletTemps;
	private JPanel panelOngletTemps;
	private SpringLayout layoutPanelOngletTemps;
	private JLabel txtpnTemps;
	private JLabel txtpnJoueur;
	private JSpinner spinner;
	private JComboBox comboBox;
	private JButton btnValiderTemps;*/

	private JButton btnDeconnexion;

	private DefaultTableModel tableur2;

	public JoueurPanelConnecte(JoueurController pController)
	{

		super();
		this.controller = pController;

		this.layout = new SpringLayout();
		this.setLayout( this.layout );

		// CRONO
		this.panelCrono = new JPanel();
		this.layout.putConstraint(SpringLayout.NORTH, this.panelCrono, 50, SpringLayout.NORTH, this);
		this.layout.putConstraint(SpringLayout.WEST, this.panelCrono, -280, SpringLayout.EAST, this);
		this.layout.putConstraint(SpringLayout.SOUTH, this.panelCrono, 160, SpringLayout.NORTH, this);
		this.layout.putConstraint(SpringLayout.EAST, this.panelCrono, -10, SpringLayout.EAST, this);
		this.add(this.panelCrono);

		this.layoutTextCrono = new SpringLayout();
		this.panelCrono.setLayout(this.layoutTextCrono);

		this.textCronoGeneral = new JLabel();
		this.layoutTextCrono.putConstraint(SpringLayout.NORTH, this.textCronoGeneral, -10, SpringLayout.NORTH, this.panelCrono);
		this.layoutTextCrono.putConstraint(SpringLayout.WEST, this.textCronoGeneral, 50, SpringLayout.WEST, this.panelCrono);
		this.layoutTextCrono.putConstraint(SpringLayout.SOUTH, this.textCronoGeneral, 50, SpringLayout.NORTH, this.panelCrono);
		this.layoutTextCrono.putConstraint(SpringLayout.EAST, this.textCronoGeneral, 0, SpringLayout.EAST, this.panelCrono);
		this.textCronoGeneral.setFont(new Font("Calibri", Font.PLAIN, 50));
		this.textCronoGeneral.setText("00:00");
		this.panelCrono.add(this.textCronoGeneral);

		this.textCronoJoueur = new JLabel();
		this.layoutTextCrono.putConstraint(SpringLayout.NORTH, this.textCronoJoueur, 50, SpringLayout.NORTH, this.panelCrono);
		this.layoutTextCrono.putConstraint(SpringLayout.WEST, this.textCronoJoueur, 115, SpringLayout.WEST, this.panelCrono);
		this.layoutTextCrono.putConstraint(SpringLayout.SOUTH, this.textCronoJoueur, -10, SpringLayout.SOUTH, this.panelCrono);
		this.layoutTextCrono.putConstraint(SpringLayout.EAST, this.textCronoJoueur, -105, SpringLayout.EAST, this.panelCrono);
		this.textCronoJoueur.setFont(new Font("Calibri", Font.PLAIN, 20));
		this.textCronoJoueur.setText("00:00");
		this.panelCrono.add(this.textCronoJoueur);


		//CHAT

		this.zoneTexte = new JTextArea();
		this.scrollPaneChat = new JScrollPane(this.zoneTexte);
		this.zoneTexte.setEditable( false );
		this.zoneTexte.setLineWrap( true );
		this.zoneTexte.setBackground( Color.white );
		this.layout.putConstraint(SpringLayout.NORTH, this.scrollPaneChat, 10, SpringLayout.SOUTH, this.panelCrono);
		this.layout.putConstraint(SpringLayout.WEST, this.scrollPaneChat, 0, SpringLayout.WEST, this.panelCrono);
		this.layout.putConstraint(SpringLayout.SOUTH, this.scrollPaneChat, 350, SpringLayout.NORTH, this);
		this.layout.putConstraint(SpringLayout.EAST, this.scrollPaneChat, -10, SpringLayout.EAST, this);
		this.add(this.scrollPaneChat,BorderLayout.CENTER);

		this.textField = new JTextField();
		this.layout.putConstraint(SpringLayout.NORTH, this.textField, 6, SpringLayout.SOUTH, this.scrollPaneChat);
		this.layout.putConstraint(SpringLayout.WEST, this.textField, 0, SpringLayout.WEST, this.panelCrono);
		this.layout.putConstraint(SpringLayout.EAST, this.textField, -90, SpringLayout.EAST, this);
		this.add(this.textField);
		this.textField.setColumns(10);

		this.btnEnvoyer = new JButton("Envoyer");
		this.layout.putConstraint(SpringLayout.NORTH, this.btnEnvoyer, -1, SpringLayout.NORTH, this.textField);
		this.layout.putConstraint(SpringLayout.WEST, this.btnEnvoyer, 6, SpringLayout.EAST, this.textField);
		this.btnEnvoyer.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				JoueurPanelConnecte.this.controller.sendMessage( JoueurPanelConnecte.this.textField.getText() );
				JoueurPanelConnecte.this.textField.setText( null );
			}
		});
		this.add(this.btnEnvoyer);

		//CAM
		/*this.labelCam = new JLabel("");
		this.layout.putConstraint(SpringLayout.NORTH, this.labelCam, 6, SpringLayout.SOUTH, this.textField);
		this.layout.putConstraint(SpringLayout.WEST, this.labelCam, 0, SpringLayout.WEST, this.panelCrono);
		this.layout.putConstraint(SpringLayout.SOUTH, this.labelCam, -34, SpringLayout.SOUTH, this);
		this.layout.putConstraint(SpringLayout.EAST, this.labelCam, -10, SpringLayout.EAST, this);
		this.add(this.labelCam);

		this.btnOnCam = new JButton("On");
		this.layout.putConstraint(SpringLayout.NORTH, this.btnOnCam, 6, SpringLayout.SOUTH, this.labelCam);
		this.layout.putConstraint(SpringLayout.EAST, this.btnOnCam, 0, SpringLayout.EAST, this.panelCrono);
		this.add(this.btnOnCam);*/

		//Définition des onglets
		this.onglets = new JTabbedPane(JTabbedPane.TOP);
		this.layout.putConstraint(SpringLayout.NORTH, this.onglets, 50, SpringLayout.NORTH, this);
		this.layout.putConstraint(SpringLayout.WEST, this.onglets, 10, SpringLayout.WEST, this);
		this.layout.putConstraint(SpringLayout.SOUTH, this.onglets, -10, SpringLayout.SOUTH, this);
		this.layout.putConstraint(SpringLayout.EAST, this.onglets, -290, SpringLayout.EAST, this);
		this.ongletIndice = new JPanel();
		this.ongletIndice.setPreferredSize(new Dimension(300, 80));
		this.onglets.addTab("Indice", this.ongletIndice);
		this.layoutOngletIndice = new SpringLayout();
		this.ongletIndice.setLayout(this.layoutOngletIndice);

		//tableau Indice
		this.paneTablIndice = new JScrollPane();
		this.layoutOngletIndice.putConstraint(SpringLayout.NORTH, this.paneTablIndice, 43, SpringLayout.NORTH, this.ongletIndice);
		this.layoutOngletIndice.putConstraint(SpringLayout.WEST, this.paneTablIndice, 20, SpringLayout.WEST, this.ongletIndice);
		this.layoutOngletIndice.putConstraint(SpringLayout.SOUTH, this.paneTablIndice, -280, SpringLayout.SOUTH, this.ongletIndice);
		this.layoutOngletIndice.putConstraint(SpringLayout.EAST, this.paneTablIndice, -10, SpringLayout.EAST, this.ongletIndice);
		this.ongletIndice.add(this.paneTablIndice);


		this.tableur = new  NonEditableDefaultTableModel();
		this.tableur.addColumn("Titre de l'indice");
		this.tableur.setRowCount(0);
		this.table = new JTable(this.tableur);
		this.paneTablIndice.setViewportView(this.table);

		/**
		 * Affichage de l'indice
		 */
		this.table.addMouseListener( new VisualisationIndice(this.table, JoueurPanelConnecte.this.controller) );


		//Achat indice
		this.panelAchatIndice = new JPanel();
		this.panelAchatIndice.setBorder(UIManager.getBorder("Tree.editorBorder"));
		this.layoutOngletIndice.putConstraint(SpringLayout.NORTH, this.panelAchatIndice, 104, SpringLayout.SOUTH, this.paneTablIndice);
		this.layoutOngletIndice.putConstraint(SpringLayout.WEST, this.panelAchatIndice, 20, SpringLayout.WEST, this.ongletIndice);
		this.layoutOngletIndice.putConstraint(SpringLayout.SOUTH, this.panelAchatIndice, 176, SpringLayout.SOUTH, this.paneTablIndice);
		this.layoutOngletIndice.putConstraint(SpringLayout.EAST, this.panelAchatIndice, -10, SpringLayout.EAST, this.ongletIndice);
		this.ongletIndice.add(this.panelAchatIndice);
		this.layoutAchatIndice = new SpringLayout();
		this.panelAchatIndice.setLayout(this.layoutAchatIndice);

		this.txtpnAcheterUnIndice = new JTextPane();
		this.layoutAchatIndice.putConstraint(SpringLayout.NORTH, this.txtpnAcheterUnIndice, 24, SpringLayout.NORTH, this.panelAchatIndice);
		this.layoutAchatIndice.putConstraint(SpringLayout.WEST, this.txtpnAcheterUnIndice, 10, SpringLayout.WEST, this.panelAchatIndice);
		this.txtpnAcheterUnIndice.setEditable(false);
		this.panelAchatIndice.add(this.txtpnAcheterUnIndice);

		this.btnValiderAchatIndice = new JButton("Valider");
		this.layoutAchatIndice.putConstraint(SpringLayout.SOUTH, this.btnValiderAchatIndice, 0, SpringLayout.SOUTH, this.txtpnAcheterUnIndice);
		this.layoutAchatIndice.putConstraint(SpringLayout.EAST, this.btnValiderAchatIndice, -10, SpringLayout.EAST, this.panelAchatIndice);
		this.panelAchatIndice.add(this.btnValiderAchatIndice);
		this.btnValiderAchatIndice.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				JoueurPanelConnecte.this.controller.buyIndice();
			}
		});

		//Onglet Temps
		/*this.ongletTemps = new JPanel();
		this.onglets.addTab("Temps", this.ongletTemps);
		this.layoutOngletTemps = new SpringLayout();
		this.ongletTemps.setLayout(this.layoutOngletTemps);

		this.panelOngletTemps = new JPanel();
		this.layoutOngletTemps.putConstraint(SpringLayout.NORTH, this.panelOngletTemps, 40, SpringLayout.NORTH, this.ongletTemps);
		this.layoutOngletTemps.putConstraint(SpringLayout.WEST, this.panelOngletTemps, 20, SpringLayout.WEST, this.ongletTemps);
		this.layoutOngletTemps.putConstraint(SpringLayout.SOUTH, this.panelOngletTemps, 150, SpringLayout.NORTH, this.ongletTemps);
		this.layoutOngletTemps.putConstraint(SpringLayout.EAST, this.panelOngletTemps, 380, SpringLayout.WEST, this.ongletTemps);
		this.ongletTemps.add(this.panelOngletTemps);
		this.layoutPanelOngletTemps = new SpringLayout();
		this.panelOngletTemps.setLayout(this.layoutPanelOngletTemps);

		this.txtpnTemps = new JLabel("Temps" , JLabel.RIGHT );
		this.layoutPanelOngletTemps.putConstraint(SpringLayout.NORTH, this.txtpnTemps, 20, SpringLayout.NORTH, this.panelOngletTemps);
		this.layoutPanelOngletTemps.putConstraint(SpringLayout.WEST, this.txtpnTemps, 100, SpringLayout.WEST, this.panelOngletTemps);
		this.panelOngletTemps.add(this.txtpnTemps);

		this.txtpnJoueur = new JLabel("Joueur" , JLabel.RIGHT );
		this.layoutPanelOngletTemps.putConstraint(SpringLayout.NORTH, this.txtpnJoueur, 10, SpringLayout.SOUTH, this.txtpnTemps);
		this.layoutPanelOngletTemps.putConstraint(SpringLayout.WEST, this.txtpnJoueur, 0, SpringLayout.WEST, this.txtpnTemps);
		this.panelOngletTemps.add(this.txtpnJoueur);

		this.spinner = new JSpinner();
		this.layoutPanelOngletTemps.putConstraint(SpringLayout.NORTH, this.spinner, 0, SpringLayout.NORTH, this.txtpnTemps);
		this.layoutPanelOngletTemps.putConstraint(SpringLayout.WEST, this.spinner, 20, SpringLayout.EAST, this.txtpnTemps);
		this.layoutPanelOngletTemps.putConstraint(SpringLayout.EAST, this.spinner, 120, SpringLayout.WEST, this.spinner);
		this.panelOngletTemps.add(this.spinner);

		this.comboBox = new JComboBox();
		this.comboBox.setModel( new DefaultComboBoxModel() );
		this.layoutPanelOngletTemps.putConstraint(SpringLayout.NORTH, this.comboBox, 0, SpringLayout.NORTH, this.txtpnJoueur );
		this.layoutPanelOngletTemps.putConstraint(SpringLayout.WEST, this.comboBox, 0, SpringLayout.WEST, this.spinner );
		this.layoutPanelOngletTemps.putConstraint(SpringLayout.EAST, this.comboBox, 120, SpringLayout.WEST, this.comboBox );
		this.panelOngletTemps.add(this.comboBox);

		this.btnValiderTemps = new JButton("Valider");
		this.layoutPanelOngletTemps.putConstraint(SpringLayout.NORTH, this.btnValiderTemps, 10, SpringLayout.SOUTH, this.comboBox);
		this.layoutPanelOngletTemps.putConstraint(SpringLayout.WEST, this.btnValiderTemps, 0, SpringLayout.WEST, this.spinner);
		this.panelOngletTemps.add(this.btnValiderTemps);
		/*this.btnValiderTemps.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				JoueurPanelConnecte.this.controller.donTemps( JoueurPanelConnecte.this.comboBox.getSelectedItem(), JoueurPanelConnecte.this.spinner.getValue() );
			}
		});*/

		this.btnDeconnexion = new JButton("Deconnexion");
		this.layout.putConstraint(SpringLayout.NORTH, this.btnDeconnexion, 10, SpringLayout.NORTH, this);
		this.layout.putConstraint(SpringLayout.WEST, this.btnDeconnexion, 10, SpringLayout.WEST, this);
		this.btnDeconnexion.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				JoueurPanelConnecte.this.zoneTexte.setText("");
				JoueurPanelConnecte.this.controller.sendDeconnexion();
			}
		});
		this.add(this.btnDeconnexion);

		this.tableur2 = new  DefaultTableModel();
		this.tableur2.addColumn("Nom (Joueur)");
		this.tableur2.addColumn("Nom (Personnage)");
		this.tableur2.addColumn("Temps restant");
		this.tableur2.addColumn("nb indices");
		this.tableur2.setRowCount(10);


		this.onglets.setOpaque(true);
		this.add(this.onglets);
	}

	/**
	 * Met à jour la vue du chrono du joueur
	 * @param pSecondesRestantes
	 */
	public void setTimeJoueur( int pSecondesRestantes )
	{
		// Décomposition en minutes/secondes
		int nbMinute = pSecondesRestantes / 60;
		pSecondesRestantes = pSecondesRestantes % 60;

		// Affichage du label général
		this.textCronoJoueur.setText(
									Outils.zeroDizaine(nbMinute) + nbMinute + ":" +
									Outils.zeroDizaine(pSecondesRestantes) + pSecondesRestantes
								);
	}

	/**
	 * Met à jour le temps du chrono général
	 * @param pSecondesRestantes
	 */
	public void setTime( int pSecondesRestantes )
	{
		this.textCronoGeneral.setText( Outils.secondToHMinSec( pSecondesRestantes ) );
	}

	/**
	 * Nettoie les champs pour entrer des entiers
	 */
	protected void netoyerInputsTemps()
	{
		netoyerInputEntier( this.fieldMinute );
	}

	/**
	 * Nettoie un input d'entier
	 */
	public static void netoyerInputEntier( JTextField pField )
	{
		if ( pField.getText().equals("") )
		{
			pField.setText("00");
		}
		else
		{
			char[] inputString = pField.getText().toCharArray();
			String outputString = "";
			for( int i = 0; i < inputString.length; i++ )
			{
				if ( Character.isDigit( inputString[i] ) )
				{
					outputString += inputString[i];
				}
			}
			pField.setText( outputString );
		}
	}

	/**
	 * Met à jour le contenu de la table joueur
	 * @param data Données à afficher dans data
	 */
	public void appendTableIndice(String[] data)
	{
		this.tableur.addRow(data);
	}

	public void updateOnglets( JoueurModel pModel, int pStatut )
	{
		if ( pStatut == JoueurView.REFRESH_INDICE
			 || pStatut == JoueurView.REFRESH_ALL )
		{
			this.tableur.setRowCount(0);
			ArrayList<String []> indices = pModel.getJoueurConnecte().getIndice();
			for(String[] str : indices)
			{
				this.appendTableIndice(str);
			}
		}
	}

	public void setPrix(int pPrix)
	{
		this.txtpnAcheterUnIndice.setText("Acheter un indice contre "+pPrix+" seconde(s).");
	}

	/**
	 * Met à jour la liste des joueurs avec qui il est possible de faire un échange de temps
	 * @param pListeJoueurs
	 * @param pJoueurConnecte
	 */
	/*public void setListeJoueur(ArrayList<String[]> pListeJoueurs, JoueurConnecte pJoueurConnecte)
	{
		for ( int i = 0; i < pListeJoueurs.size(); i++ )
		{
			if ( ! pJoueurConnecte.getNom().equals( pListeJoueurs.get(i)[0] )
				 && ! pJoueurConnecte.getPrenom().equals( pListeJoueurs.get(i)[1] ) )
			{
				this.comboBox.addItem( pListeJoueurs.get(i)[0] + " " + pListeJoueurs.get(i)[1] );
			}
		}
	}*/

	public JTextArea getChat()
	{
		return this.zoneTexte;
	}

	public ChronoGeneral getChrono()
	{
		return this.getChrono();
	}
}
